﻿using Dapper;
using IntelligentGateway.Model.Platform;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentGateway.Data
{
    public class PersonInfoDA
    {
        public bool InsertUpdate(PersonInfo Info)
        {                 
            bool update = CheckExistPerson(Info.personID);
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    int count = 0;
                    if (update)
                    {
                        string sql = "Update personInfo set personType=@personType,cardNO=@cardNO,frontFloor=@frontFloor,backFloor=@backFloor,startTime=@startTime,endTime=@endTime where personID=@personID";

                        count = conn.Execute(sql, Info);
                    }
                    else
                    {
                        string sql = "Insert personInfo(personID,personType,cardNO,frontFloor,backFloor,startTime,endTime) values(@personID,@personType,@cardNO,@frontFloor,@backFloor,@startTime,@endTime)";

                        count = conn.Execute(sql, Info);
                    }
                    return count > 0;
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }
        public bool DeletePerson(string PersonID)
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    int count = conn.Execute("Delete FROM PersonInfo where PersonID=@PersonID", new { PersonID = PersonID });
                    return true;                  
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }
        public bool CheckExistPerson(string PersonID)
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    int count = conn.ExecuteScalar<int>("SELECT ID FROM PersonInfo where PersonID=@PersonID",new { PersonID = PersonID });
                    if (count > 0)
                        return true;
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e)
                {
                    return false;
                }
            }
        }
        public List<PersonInfo> GetAllPerson()
        {
            using (var conn = DataFactory.GetMYSQLConnection())
            {
                try
                {
                    List<PersonInfo> infos = conn.Query<PersonInfo>("SELECT PersonID FROM PersonInfo").AsList();
                    return infos;
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }
    }
}
